import {Component, EventEmitter, Input, Output} from '@angular/core';
import {FileObj} from "@common/FileObj";
import {ActionSheetController} from "ionic-angular";
import {NativeProvider} from "@providers/natives/native";
import {WechatJsSdkProvider} from "@providers/wechat-js-sdk";

/**
 * Generated class for the SelectPicComponent component.
 *
 * See https://angular.io/api/core/Component for more info on Angular
 * Components.
 */
@Component({
  selector: 'select-pic',
  templateUrl: 'select-pic.html'
})
export class SelectPicComponent {
  @Input() max: number = 4;  //最多可选择多少张图片，默认为4张

  @Input() allowAdd: boolean = true;  //是否允许新增

  @Input() allowDelete: boolean = true;  //是否允许删除

  @Input() fileObjList: FileObj[] = [];   //图片列表,与fileObjListChange形成双向数据绑定
  @Output() fileObjListChange = new EventEmitter<any>();
  @Output() addEvent = new EventEmitter<any>();//增加操作
  @Output() deleteEvent = new EventEmitter<any>();//删除操作
  constructor(private actionSheetCtrl: ActionSheetController,
              private native: NativeProvider,
              private wechat: WechatJsSdkProvider) {
  }


  ngOnChanges() {
    if (!this.fileObjList) {
      this.fileObjList = [];
    }
  }

  addPicture() {
    if (this.wechat.isWechat()) {
      this.wechat.chooseImage({
        // count: (this.max - this.fileObjList.length)
      }).then(res => {
        for (let img of res) {
          let fileObj = <FileObj>{'origPath': img, 'thumbPath': img};
          this.fileObjList.push(fileObj);
          this.fileObjListChange.emit(this.fileObjList);
          this.addEvent.emit(this.fileObjList);
        }
        this.addEvent.emit({'list': this.fileObjList});
      });
    } else {
      this.native.getMultiplePicture({'maximumImagesCount': this.max}).subscribe(urls => {
        urls.forEach(element => {
          this.fileObjList.push(<FileObj>{'origPath': element, 'thumbPath': element});
        });
      }, error => {
        // alert(error);
      });
    }
  }

  deletePicture(i) {//删除照片
    if (!this.allowDelete) {
      return;
    }
    let that = this;
    that.actionSheetCtrl.create({
      buttons: [
        {
          text: '删除',
          role: 'destructive',
          handler: () => {
            this.deleteEvent.emit(that.fileObjList[i]);
            that.fileObjList.splice(i, 1);
            this.fileObjListChange.emit(this.fileObjList);
            //this.deleteEvent.emit({ 'index': i });
          }
        },
        {
          text: '取消',
          role: 'cancel'
        }
      ]
    }).present();
  }

  viewerPicture(index) {//照片预览
    let urls = [], current = '';
    for (let i = 0, len = this.fileObjList.length; i < len; i++) {
      let origPath = this.fileObjList[i].origPath;
      if (!origPath) origPath = "" + this.fileObjList[i];
      if (i == index) {
        current = origPath;
      }
      urls.push(origPath);
    }
    if (this.wechat.isWechat()) {
      this.wechat.previewImage(current, urls);
    } else {
      this.native.previewImage(current);
    }
  }

}
